package com.song.secure.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.stereotype.Repository;

import com.song.secure.bean.User;
@Repository
public class RoleServiceImpl implements RoleService {
	
//	@Resource
//	private JdbcTemplate jdbcTemplate;
	
	
	
	public Map<String, Collection<ConfigAttribute>> getAllConfigAttribute() {
		String sql ="select  *  from sys_role srl , sys_role_right srr , sys_right  srt "
				      + " where srl.id = srr.role_id and  srr.right_id = srt.id";
		
		Map<String, Collection<ConfigAttribute>> attibs = new  HashMap<String, Collection<ConfigAttribute>>(); 
		Collection<ConfigAttribute> atttrib1 = new ArrayList<ConfigAttribute>();
		Collection<ConfigAttribute> atttrib2 = new ArrayList<ConfigAttribute>();
		atttrib1.add(new SecurityConfig("ROLE_ADMIN"));
		atttrib2.add(new SecurityConfig("ROLE_USER"));
		attibs.put("/u/prehelloword", atttrib1);
//		List<Map<String,Object>> mapps =   jdbcTemplate.queryForList(sql);
//		
//		for(Map<String,Object>  map :mapps)
//		{
//			String url = map.get("right_url").toString();
//			String role = map.get("role_security").toString();
//			if(attibs.containsKey(url))
//			{
//				Collection<ConfigAttribute> data =  attibs.get(url);
//				data.add(new SecurityConfig(role));
//				attibs.put(url,data);
//			}else{
//				Collection<ConfigAttribute> data = new HashSet<ConfigAttribute>();
//				data.add(new SecurityConfig(role));
//				attibs.put(url,data);
//			}
//		}
		
		
		return attibs;
	}


//	public JdbcTemplate getJdbcTemplate() {
//		return jdbcTemplate;
//	}
//
//
//	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//		this.jdbcTemplate = jdbcTemplate;
//	}


	
	public User getUserByName(String name) {
		
//		String sql ="select * from sys_user where usr_name =? ";
//		
//		Map<String,Object> map  = this.jdbcTemplate.queryForMap(sql, name);
//		
//		User usr = new User(map.get("usr_name").toString(), map.get("usr_password").toString() );
//		
//		int role_id = Integer.parseInt( map.get("usr_role_id").toString());
//		
//		
//		String rsql ="select role_security from sys_role where id =?";
//		
//		String role  =	this.jdbcTemplate.queryForMap(rsql, role_id).get("role_security").toString();
//		GrantedAuthority authority = new GrantedAuthorityImpl(role);
//		Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
//		authSet.add(authority);
//		usr.setAuthorities(authSet);
		User user = null; 
		if(name.equals("song"))
		{
			user  = new User("song","song"); 
			GrantedAuthority authority = new GrantedAuthorityImpl("ROLE_ADMIN");
			Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
			authSet.add(authority);
			user.setAuthorities(authSet);
		}else{
			user = new User("yang", "yang");
			GrantedAuthority authority = new GrantedAuthorityImpl("ROLE_USER");
			Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
			authSet.add(authority);
			user.setAuthorities(authSet);
		}
		
		return user;
	}

	
	
	
}
